const app = getApp();
import { townshipList, province_Data, municipalLevel_Data, county_Data, township_Data } from "../../../api/homePage"
import { setStorage, getStorage } from "../../../utils/localStorage"
import { getAddress } from "../../../utils/address"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navData: {
            level: 2,
            title: '选择乡镇'
        },
        reData: [],
        navIndex: 0,
        MunicipalLevel: [],  //市级下区县乡镇数据
        provinceData: [], //省份数据
        municipalData: [], //市级数据
        countyData: [], //获取县级数据
        user: getStorage("user"),
        townName: null, //乡镇名称,
        addressPrice: "浙江省", //省份
        addressName: "金华市",//市
        addressCounty: "", //县份
        currentName: "", //当前定位省,市,乡镇级别
        currentNameTown: "", //乡镇级别
        navMargin: app.globalData.navHeight + app.globalData.navTop,
    },
    onLoad(options) {
        this.setData({
            addressName: options.name.replace(/,/, "-"),
            user: getStorage("user"),
            townName: getStorage("user").townShipName.slice(getStorage("user").townShipName.lastIndexOf(",") + 1, getStorage("user").townShipName.length),
        });
        app.globalData.detailPageType = "选择乡镇";  //详情页类型
        this.getCurrentAddres();
        this.gettownshipList();
        this.getProvinceData();
        this.getCountyData(options.id);
    },
    onReady() {

    },
    onShow() {

    },
    //获取热门乡镇
    gettownshipList() {
        townshipList().then(res => {
            res.data.data.forEach(item => {
                item.townName = item.name.slice(item.name.lastIndexOf(",") + 1, item.name.length);
            });
            this.setData({
                reData: res.data.data
            });
           
        })
    },
    //获取省份数据
    getProvinceData() {
        province_Data().then(res => {
            res.data.data.forEach((item, index) => {
                Object.assign(item, { flag: "false" })
            });
            this.setData({
                provinceData: res.data.data
            });
        });
    },
    //获取市级数据
    getMunicipalLevelData(id) {
        municipalLevel_Data({ id: id }).then(res => {
            this.setData({
                municipalData: res.data.data
            });
        });
    },
    navClick(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            navIndex: index,
        })
    },
    //获取当前定位
    getCurrentAddres() {
        getAddress().then(res => {
            let province = res.result.address_component.province,
                city = res.result.address_component.city,
                street = res.result.address_component.street;
            this.setData({
                currentName: province + "," + city + "," + street,
                currentNameTown:street,
            });
        });
    },
    //当前位置点击事件
    currentBind(e) {
        let item = e.currentTarget.dataset.item;
        app.globalData.townName = item;
        app.globalData.detailPageType = '已选择乡镇';
        wx.navigateBack();
    },
    //热门乡镇点击事件
    rmtownClick(e) {
        let item = e.currentTarget.dataset.item;
        app.globalData.townName = item.name;
        app.globalData.detailPageType = '已选择乡镇';
        wx.navigateBack();
    },
    //其它乡镇点击事件
    qttownClick(e) {
        let that = this.data;
        let val = e.currentTarget.dataset.val;
        app.globalData.townName = that.addressPrice + "," + that.addressName + "," + val;
        app.globalData.detailPageType = '已选择乡镇';
        wx.navigateBack();
    },
    //其它乡镇展开图标点击事件
    shrinkTap(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        let value = "MunicipalLevel[" + index + "].flag"
        this.setData({
            [value]: item.flag ? false : true
        });
    },
    //其它地区下的 省份点击事件
    provinceTap(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        this.setData({
            municipalData: [],
            addressPrice: item.name,
        });
        this.data.provinceData.forEach((val, i) => {
            let value = "provinceData[" + i + "].flag";
            if (index === i) {
                this.setData({
                    [value]: item.flag === "true" ? "false" : "true"
                });
            } else {
                this.setData({
                    [value]: "false"
                });
            }
        });
        console.log(item, '省份点击事件');
        this.getMunicipalLevelData(item.id);
    },
    //其它地区下的 市级点击事件
    municipalTap(e) {
        let item = e.currentTarget.dataset.item,
            index = e.currentTarget.dataset.index;
        county_Data({ id: item.id }).then(res => {
            res.data.data.forEach(item => {
                item.data = item.data.split(",");
            });
            this.setData({
                addressName: item.name,
                MunicipalLevel: res.data.data,
                navIndex: 0,
            });
        })

        this.pageTop();
    },
    //获取市级下区县乡镇的数据
    getCountyData(id) {
        county_Data({ id: id }).then(res => {
            res.data.data.forEach(item => {
                item.data = item.data.split(",");
            });
            this.setData({
                MunicipalLevel: res.data.data,
            });
        });
    },
    //返回到页面顶部
    pageTop() {
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})